package com.three.crm.tencentcloud;

import android.content.Context;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;
import com.tencent.qcloud.core.auth.BasicLifecycleCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudLifecycleCredentials;
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;
import com.tencent.qcloud.core.common.QCloudClientException;

import java.util.HashMap;
import java.util.Map;

public class CosServiceFactory {
    private static final String defaultRegion = BaseConfig.REGION;

    private static Map<String, CosXmlService> cosXmlServiceMap = new HashMap<>();

    public static CosXmlService getCosXmlService(Context context, String region, final String secretId,
                                                 final String secretKey, final String sessionToken, final long startTime, final long expiredTime,boolean refresh) {
        if (refresh) {
            cosXmlServiceMap.remove(region);
        }

        CosXmlService cosXmlService = cosXmlServiceMap.get(region);

        if (cosXmlService == null) {
            CosXmlServiceConfig cosXmlServiceConfig = getCosXmlServiceConfig(region);
            QCloudCredentialProvider qCloudCredentialProvider = getCredentialProviderWithIdAndKey(secretId, secretKey);
            /* 获取默认签名CosXmlService实例 */
            cosXmlService = getCosXmlService(context, cosXmlServiceConfig, qCloudCredentialProvider);
            /* 获取自定义签名CosXmlService实例 */
//            cosXmlService = getCosXmlServiceByCustomSignature(context, cosXmlServiceConfig, qCloudCredentialProvider);

            cosXmlServiceMap.put(region, cosXmlService);
        }

        return cosXmlService;
    }

    private static QCloudCredentialProvider getCredentialProviderWithIdAndKey(String secretId, String secretKey) {
        /**
         * 注意！注意！注意！
         * 由于该方式会存在泄漏密钥的风险，我们强烈不推荐您使用这种方式，建议您仅在安全的环境下临时测试时使用.
         * 建议采用"通过临时秘钥进行授权"，请参考：https://cloud.tencent.com/document/product/436/12159#.E5.88.9D.E5.A7.8B.E5.8C.96.E6.9C.8D.E5.8A.A1
         */
        return new ShortTimeCredentialProvider(secretId, secretKey, 300);
    }

    public static CosXmlService getCosXmlService(Context context, String secretId, String secretKey,String sessionToken,long startTime,long expiredTime, boolean refresh) {
        return getCosXmlService(context, defaultRegion, secretId, secretKey,sessionToken,startTime,expiredTime, refresh);
    }

    /**
     * 获取配置类
     */
    private static CosXmlServiceConfig getCosXmlServiceConfig(String region) {
        return new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .setDebuggable(true)
//                .setHostFormat("www.jordanqin.cn")
                .isHttps(true)
                .builder();
    }

    /**
     * 获取QCloudCredentialProvider对象，来给 SDK 提供临时密钥
     * @parma secretId 永久密钥 secretId
     * @param secretKey 永久密钥 secretKey
     */
    private static QCloudCredentialProvider getCredentialProvider(String secretId, String secretKey, String sessionToken, long startTime, long expiredTime) {
        return new BasicLifecycleCredentialProvider() {
            @Override
            protected QCloudLifecycleCredentials fetchNewCredentials() throws QCloudClientException {
                return new SessionQCloudCredentials(secretId,secretKey,sessionToken,startTime,expiredTime);
            }
        };
    }

    /**
     * 获取默认签名CosXmlService实例
     */
    private static CosXmlService getCosXmlService(
            Context context,
            CosXmlServiceConfig cosXmlServiceConfig,
            QCloudCredentialProvider qCloudCredentialProvider
    ){
        return new CosXmlService(context, cosXmlServiceConfig, qCloudCredentialProvider);
    }

    public static TransferManager getTransferManager(Context context, String region, final String secretId,
            final String secretKey, final String sessionToken, final long startTime, final long expiredTime,boolean refresh
    ){
        CosXmlService cosXmlService = getCosXmlService(context,secretId,secretKey,sessionToken,startTime,expiredTime,refresh);
        TransferConfig transferConfig = new TransferConfig.Builder().build();
        return new TransferManager(cosXmlService,transferConfig);
    }


}
